{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a3d7067",
   "metadata": {},
   "outputs": [],
   "source": [
    "from hashlib import sha256"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0849457e",
   "metadata": {},
   "outputs": [],
   "source": [
    "encrypted_elements = [b'217',b'354',b'466']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a74a9600",
   "metadata": {},
   "source": [
    "## Simple Bloom Encoding Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad5dd978",
   "metadata": {},
   "outputs": [],
   "source": [
    "num_bits = 32\n",
    "num_hashfunctions = 4\n",
    "filterlist = ['0'] * num_bits\n",
    "\n",
    "for element in encrypted_elements:\n",
    "    element1 = str.encode('1') + element\n",
    "    k = sha256(element1).hexdigest()\n",
    "    h1 = int(k,16) % num_bits\n",
    "\n",
    "    element2 = str.encode('2') + element\n",
    "    k = sha256(element2).hexdigest()\n",
    "    h2 = int(k,16) % num_bits\n",
    "    \n",
    "    for i in range(num_hashfunctions):\n",
    "        pos = ((h1 + i * h2) % num_bits)\n",
    "        filterlist[num_bits-1-pos]='1'\n",
    "        \n",
    "filterstring = ''.join(filterlist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33ae9247",
   "metadata": {},
   "outputs": [],
   "source": [
    "filterstring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00634579",
   "metadata": {},
   "outputs": [],
   "source": [
    "for number in range(50):\n",
    "    check = True\n",
    "\n",
    "    element1 = str.encode('1') + str.encode('{0:d}'.format(number))\n",
    "    k = sha256(element1).hexdigest()\n",
    "    h1 = int(k,16) % num_bits\n",
    "\n",
    "    element2 = str.encode('2') + str.encode('{0:d}'.format(number))\n",
    "    k = sha256(element2).hexdigest()\n",
    "    h2 = int(k,16) % num_bits\n",
    "    \n",
    "    for i in range(num_hashfunctions):\n",
    "        pos = ((h1 + i * h2) % num_bits)\n",
    "        if (filterlist[num_bits-1-pos]=='1'):\n",
    "            check = check & True\n",
    "        else:\n",
    "            check = False\n",
    "    if check==True:\n",
    "        print(number)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82e75576",
   "metadata": {},
   "source": [
    "## Simple GCS Encoding Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e31f396",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "gcshash_range = 128\n",
    "gcsdiv = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c1555ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "# For all encrypted elements, calculate hash and then bucket value\n",
    "ulist = []\n",
    "for element in encrypted_elements:\n",
    "    k = sha256(element).hexdigest()\n",
    "    ks = int(k,16) % gcshash_range\n",
    "    print(ks, k)\n",
    "    ulist.append(ks)\n",
    "\n",
    "# Sort the hash bucket values\n",
    "ulist.sort()\n",
    "# Calculate deltas between sorted hash bucket values \n",
    "udiff = [ulist[0]] + [ulist[n]-ulist[n-1] for n in range(1,len(ulist))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eebec87e",
   "metadata": {},
   "outputs": [],
   "source": [
    "ulist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20b568db",
   "metadata": {},
   "outputs": [],
   "source": [
    "udiff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08ba561c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# For all delta hash bucket values encode as unary portion for quotient followed by binary for remainder.\n",
    "# Pad with leading zeros so binary portion is of consistent length.\n",
    "# Concatenate with previous values\n",
    "\n",
    "encoded = ''\n",
    "for diff in udiff:\n",
    "    if diff != 0:\n",
    "        quot = int(diff / pow(2,gcsdiv))\n",
    "        rem = diff % pow(2,gcsdiv)\n",
    "        next = '{0:b}'.format(rem) + '1' + ('0' * quot)\n",
    "        pad = next.zfill(quot+gcsdiv+1)\n",
    "        print(quot,rem, next, pad)\n",
    "        encoded = pad + encoded"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c07e46b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "encoded"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0110bd9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
